﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;

namespace simDC
{
    public partial class Memory : Form
    {
        DataTable dataMemT = new DataTable();
        DataTable codeMemT = new DataTable();
        DataTable stackMemT = new DataTable();
        DataTable intrMemT = new DataTable();
        Processor p = Processor.P;
        public Thread localThred = null;

        public Memory()
        {
            InitializeComponent();

           /* dataMemT.Columns.Add("Address", typeof(String));
            dataMemT.Columns.Add("Value", typeof(String));
            dataGridTable.DataSource = dataMemT;

            codeMemT.Columns.Add("Address", typeof(String));
            codeMemT.Columns.Add("Value", typeof(String));
            codeGridTable.DataSource = codeMemT;

            stackMemT.Columns.Add("Address", typeof(String));
            stackMemT.Columns.Add("Value", typeof(String));
            stackGridTable.DataSource = stackMemT;

            intrMemT.Columns.Add("Address", typeof(String));
            intrMemT.Columns.Add("Value", typeof(String));
            intrGridTable.DataSource = intrMemT;*/

            datalistView.View = View.Details;
            datalistView.Columns.Add("Address", 50, HorizontalAlignment.Center);
            datalistView.Columns.Add("Value - HEX", 80, HorizontalAlignment.Center);
            datalistView.Columns.Add("Value - DEC", 80, HorizontalAlignment.Center);

            codelistView.View = View.Details;
            codelistView.Columns.Add("Address", 50, HorizontalAlignment.Center);
            codelistView.Columns.Add("Value - HEX", 80, HorizontalAlignment.Center);
            codelistView.Columns.Add("Value - DEC", 80, HorizontalAlignment.Center);

            stacklistView.View = View.Details;
            stacklistView.Columns.Add("Address", 50, HorizontalAlignment.Center);
            stacklistView.Columns.Add("Value - HEX", 80, HorizontalAlignment.Center);
            stacklistView.Columns.Add("Value - DEC", 80, HorizontalAlignment.Center);

            intrlistView.View = View.Details;
            intrlistView.Columns.Add("Address", 50, HorizontalAlignment.Center);
            intrlistView.Columns.Add("Value - HEX", 80, HorizontalAlignment.Center);
            intrlistView.Columns.Add("Value - DEC", 80, HorizontalAlignment.Center);


            string[] vect = new string[3];
            ListViewItem lvi = null;

            for (int i = 0; i < p.dataMemory.Length; i++)
            {
                vect[0] = "0x" + (2 * i).ToString("X4");
                vect[1] = p.dataMemory[i].ToString("X4");
                vect[1] = p.dataMemory[i].ToString();
                lvi = new ListViewItem(vect);
                datalistView.Items.Add(lvi);
            }

            CheckForIllegalCrossThreadCalls = false;
            localThred = new Thread(UpdateMem);
            localThred.Start();           
                       
        }
        public void UpdateMem()
        {
            while (true)
            {
                Simulator.updateMemory.WaitOne();
                MyRefresh();
                Simulator.updateMemory.Reset();
            }
        }
        public void MyRefresh() 
        {
            try
            {/*
                dataMemT.Clear();
                codeMemT.Clear();
                stackMemT.Clear();
                intrMemT.Clear();

                for (int i = 0; i < p.dataMemory.Length; i++)
                    dataMemT.Rows.Add("0x" + (2 * i).ToString("X4"), p.dataMemory[i]);
                for (int i = 0; i < p.codeMemory.Length; i++)
                    codeMemT.Rows.Add("0x" + (2 * i).ToString("X4"), "0x" + p.codeMemory[i].ToString("X4"));
                for (int i = p.stackMemory.Length - 1; i >= 0; i--)
                    stackMemT.Rows.Add("0x" + (2 * i).ToString("X4"), p.stackMemory[i]);
                for (int i = 0; i < p.intMemory.Length; i++)
                    intrMemT.Rows.Add("0x" + (2 * i).ToString("X4"), "0x" + p.intMemory[i].ToString("X4"));*/

                datalistView.Items.Clear();
                codelistView.Items.Clear();
                stacklistView.Items.Clear();
                intrlistView.Items.Clear();

                string[] vect = new string[3];
                
                ListViewItem lvi = null;

                for (int i = 0; i < p.dataMemory.Length; i++)
                {
                     vect[0] = "0x" + (2 * i).ToString("X4");
                     vect[1] = p.dataMemory[i].ToString("X4");
                     vect[2] = p.dataMemory[i].ToString("D1");
                    lvi = new ListViewItem(vect);
                     datalistView.Items.Add(lvi);                
                }
                for (int i = 0; i < p.codeMemory.Length; i++)
                {
                    vect[0] = "0x" + (2 * i).ToString("X4");
                    vect[1] = p.codeMemory[i].ToString("X4");
                    vect[2] = p.codeMemory[i].ToString("D1");
                    lvi = new ListViewItem(vect);
                    codelistView.Items.Add(lvi);
                }
                for (int i = p.stackMemory.Length - 1; i >= 0; i--)
                {
                    vect[0] = "0x" + (2 * i).ToString("X4");
                    vect[1] = p.stackMemory[i].ToString("X4");
                    vect[2] = p.stackMemory[i].ToString("D1");
                    lvi = new ListViewItem(vect);
                    stacklistView.Items.Add(lvi);
                }
                for (int i = 0; i < p.intMemory.Length; i++)
                {
                    vect[0] = "0x" + (2 * i).ToString("X4");
                    vect[1] = p.intMemory[i].ToString("X4");
                    vect[2] = p.intMemory[i].ToString("D4");
                    lvi = new ListViewItem(vect);
                    intrlistView.Items.Add(lvi);
                } 
               

            }
            catch 
            {
            
            }
        
        }

        private void button1_Click(object sender, EventArgs e)
        {
            MyRefresh();
        }

        private void Memory_FormClosed(object sender, FormClosedEventArgs e)
        {
            
            
        }

        private void Memory_Load(object sender, EventArgs e)
        {

        }

        private void Memory_FormClosing(object sender, FormClosingEventArgs e)
        {          
                this.Hide();
                e.Cancel = true;               
           
        }

        private void refreshToolStripMenuItem_Click(object sender, EventArgs e)
        {
            MyRefresh();
        }

        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            MyRefresh();
        }

        private void refreshToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            MyRefresh();
        }

        private void cevaToolStripMenuItem_Click(object sender, EventArgs e)
        {

        }

        
    }
}
